package com.fr.chart.plot;

import com.fr.base.DefaultValues;
import com.fr.base.FRContext;
import com.fr.base.FRFont;
import com.fr.base.core.DoubleDimension2D;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.axis.TextAttr;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.glyph.SoloGlyph;
import com.fr.chart.core.glyph.TextGlyph;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.util.Utils;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.LineMetrics;
import java.awt.geom.Arc2D.Double;
import java.awt.geom.Dimension2D;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.math.BigDecimal;
import java.text.Format;

public class Meter extends SoloGlyph
{
  private static final long serialVersionUID = 3950923513149375891L;
  public static final String XML_TAG = "Meter";
  private double value;
  private TextGlyph titleGlyph;
  private MeterStyle meterStyle;
  private Rectangle2D bounds;
  private transient Rectangle2D meterArea;
  private transient double meterMiddleX;
  private transient double meterMiddleY;
  private transient double radius;
  private transient Rectangle2D roundBounds;

  public Meter()
  {
  }

  public Meter(TextGlyph paramTextGlyph, double paramDouble, MeterStyle paramMeterStyle)
  {
    setTitle(paramTextGlyph);
    setValue(paramDouble);
    this.meterStyle = paramMeterStyle;
  }

  public void setBounds(Rectangle2D paramRectangle2D)
  {
    this.bounds = paramRectangle2D;
  }

  public Rectangle2D getBounds()
  {
    return this.bounds;
  }

  public void draw(Graphics paramGraphics)
  {
    initTransientValue();
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    localGraphics2D.translate(this.bounds.getX(), this.bounds.getY());
    localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    fillBackground(localGraphics2D);
    drawTicks(localGraphics2D);
    int i = 0;
    int j = this.meterStyle.getIntervalCount();
    while (i < j)
    {
      drawArcForInterval(localGraphics2D, this.meterStyle.getInterval(i));
      ++i;
    }
    drawValueLabel(localGraphics2D);
    drawArrow(localGraphics2D);
    if (getTitle() != null)
      getTitle().draw(localGraphics2D);
    localGraphics2D.translate(-this.bounds.getX(), -this.bounds.getY());
  }

  private void initTransientValue()
  {
    double d1 = 6.0D;
    double d2 = 6.0D;
    double d3 = 4.0D + d1 / 2.0D;
    double d4 = 4.0D + d2 / 2.0D;
    double d5 = this.bounds.getWidth() - 8.0D - d1;
    double d6 = this.bounds.getHeight() - 8.0D - d2 + (((getMeterStyle().getMeterAngle() <= 180) && (!(getMeterStyle().getDialShape().equals("CIRCLE")))) ? this.bounds.getHeight() / 1.25D : 0.0D);
    double d7 = Math.min(d5, d6) / 2.0D;
    d3 = (d3 + d3 + d5) / 2.0D - d7;
    d4 = (d4 + d4 + d6) / 2.0D - d7;
    d5 = 2.0D * d7;
    d6 = 2.0D * d7;
    this.meterArea = new Rectangle2D.Double(d3, d4, d5, d6);
    this.meterMiddleX = this.meterArea.getCenterX();
    this.meterMiddleY = this.meterArea.getCenterY();
    this.radius = (this.meterArea.getWidth() / 2.0D + 3.0D);
    this.roundBounds = new Rectangle2D.Double(this.meterMiddleX - this.radius, this.meterMiddleY - this.radius, 2.0D * this.radius, 2.0D * this.radius);
  }

  private void fillBackground(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Color localColor = this.meterStyle.getDialBackgroundColor();
    if (localColor != null)
    {
      localGraphics2D.setPaint(localColor);
      double d1 = valueToAngle(this.meterStyle.getStartValue());
      double d2 = valueToAngle(this.meterStyle.getEndValue()) - d1;
      int i = 0;
      if (this.meterStyle.getDialShape().equals("PIE"))
      {
        if (this.meterStyle.getMeterAngle() > 180)
          i = 1;
        else
          i = 2;
      }
      else if (this.meterStyle.getDialShape().equals("CIRCLE"))
      {
        i = 2;
        d2 = 360.0D;
      }
      else
      {
        throw new IllegalStateException("DialShape not recognised.");
      }
      Arc2D.Double localDouble = new Arc2D.Double(this.roundBounds, d1, d2, i);
      localGraphics2D.fill(localDouble);
      localGraphics2D.setPaint(new Color(182, 182, 182));
      Stroke localStroke = localGraphics2D.getStroke();
      localGraphics2D.setStroke(new BasicStroke(2.0F, 1, 1));
      localGraphics2D.drawArc((int)(this.meterMiddleX - this.radius), (int)(this.meterMiddleY - this.radius), (int)(2.0D * this.radius), (int)(2.0D * this.radius), (int)d1, (int)d2);
      localGraphics2D.setStroke(localStroke);
      double d3 = this.radius * 0.6D + 1.0D;
      Rectangle2D.Double localDouble1 = new Rectangle2D.Double(this.meterMiddleX - d3, this.meterMiddleY - d3, 2.0D * d3, 2.0D * d3);
      localGraphics2D.fill(new Arc2D.Double(localDouble1, d1, d2, i));
    }
    localGraphics2D.setPaint(localPaint);
  }

  private void drawArrow(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    Stroke localStroke = localGraphics2D.getStroke();
    localGraphics2D.setPaint(this.meterStyle.getNeedleColor());
    double d1 = (this.radius * 0.1D > 8.0D) ? 8.0D : this.radius * 0.1D;
    localGraphics2D.fill(new Arc2D.Double(this.meterMiddleX - d1, this.meterMiddleY - d1, d1 * 2.0D, d1 * 2.0D, 0.0D, 360.0D, 1));
    if ((getValue() >= this.meterStyle.getStartValue()) && (getValue() <= this.meterStyle.getEndValue()))
    {
      localGraphics2D.setStroke(new BasicStroke(2.0F, 1, 1));
      double d2 = this.meterMiddleX + 0.975D * this.radius * Math.cos(3.141592653589793D * valueToAngle(getValue()) / 180.0D);
      double d3 = this.meterMiddleY - 0.975D * this.radius * Math.sin(3.141592653589793D * valueToAngle(getValue()) / 180.0D);
      double d4 = this.meterMiddleX - (d1 + 5.0D) * Math.cos(3.141592653589793D * valueToAngle(getValue()) / 180.0D);
      double d5 = this.meterMiddleY + (d1 + 5.0D) * Math.sin(3.141592653589793D * valueToAngle(getValue()) / 180.0D);
      localGraphics2D.draw(new Line2D.Double(d4, d5, d2, d3));
    }
    localGraphics2D.setPaint(localPaint);
    localGraphics2D.setStroke(localStroke);
  }

  public Shape getShape()
  {
    return getBounds();
  }

  private double valueToAngle(double paramDouble)
  {
    paramDouble -= getMeterStyle().getStartValue();
    double d = getMeterStyle().getMeterAngle() / 2 + 90;
    return (d - paramDouble / (getMeterStyle().getEndValue() - getMeterStyle().getStartValue()) * getMeterStyle().getMeterAngle());
  }

  private void drawValueLabel(Graphics2D paramGraphics2D)
  {
    String str = "No value";
    Format localFormat = this.meterStyle.getUnitFormat();
    if (localFormat != null)
      str = localFormat.format(Double.valueOf(Utils.doubleToString(getValue()))) + " " + this.meterStyle.getUnits();
    else
      str = getValue() + " " + this.meterStyle.getUnits();
    TextAttr localTextAttr = new TextAttr(getFont(new Dimension((int)(this.radius * 0.5D), (int)(this.radius * 0.2D)), str));
    Dimension2D localDimension2D = TextGlyph.calculateTextDimension(str, localTextAttr);
    double d = (this.meterArea.getHeight() / 10.0D > 20.0D) ? 20.0D : this.meterArea.getHeight() / 10.0D;
    Rectangle2D.Double localDouble = new Rectangle2D.Double(this.meterMiddleX - localDimension2D.getWidth() / 2.0D, this.meterMiddleY + d, localDimension2D.getWidth(), localDimension2D.getHeight());
    TextGlyph.drawLabel(paramGraphics2D, str, localTextAttr, localDouble);
  }

  private void drawArcForInterval(Graphics2D paramGraphics2D, MeterInterval paramMeterInterval)
  {
    Paint localPaint = paramGraphics2D.getPaint();
    Stroke localStroke = paramGraphics2D.getStroke();
    paramGraphics2D.setPaint(paramMeterInterval.getBackgroundColor());
    double d1 = valueToAngle(paramMeterInterval.getBeginValue());
    double d2 = valueToAngle(paramMeterInterval.getEndValue());
    paramGraphics2D.setStroke(new BasicStroke(2.0F, 1, 1));
    float f = (float)(this.radius * 0.6D);
    while (f < this.radius * 0.6D + paramMeterInterval.getOutlineSize())
    {
      Rectangle2D.Double localDouble = new Rectangle2D.Double(this.meterMiddleX - f, this.meterMiddleY - f, 2.0F * f, 2.0F * f);
      paramGraphics2D.draw(new Arc2D.Double(localDouble, d1, d2 - d1, 0));
      f += 1.0F;
    }
    paramGraphics2D.setStroke(localStroke);
    paramGraphics2D.setPaint(localPaint);
  }

  private void drawTicks(Graphics2D paramGraphics2D)
  {
    double d1 = this.meterStyle.getTickSize();
    if (d1 != 0.0D)
    {
      Paint localPaint = paramGraphics2D.getPaint();
      paramGraphics2D.setPaint(this.meterStyle.getTickColor());
      double d2 = this.meterStyle.getStartValue();
      double d3 = this.meterStyle.getEndValue();
      FRFont localFRFont = getTickLabelFont();
      double d4 = (this.radius - 8.0D < 0.95D * this.radius) ? this.radius - 8.0D : 0.95D * this.radius;
      BigDecimal localBigDecimal = new BigDecimal(Double.toString(d2)).subtract(new BigDecimal(Double.toString(d1)));
      double d5 = d2;
      while (d5 <= d3)
      {
        localBigDecimal = localBigDecimal.add(new BigDecimal(Double.toString(d1)));
        drawTickLine(paramGraphics2D, d5, d4, this.meterStyle.getTickColor());
        drawTickLabel(paramGraphics2D, localBigDecimal.doubleValue(), 0.85D * this.radius, localFRFont);
        d5 += d1;
      }
      d5 = (this.radius - 5.0D < 0.95D * this.radius) ? this.radius - 5.0D : 0.95D * this.radius;
      double d6 = d2;
      while (d6 <= d3)
      {
        if ((d6 - d2) % d1 == 0.0D)
          break label283:
        drawTickLine(paramGraphics2D, d6, d5, this.meterStyle.getTickColor());
        label283: d6 += d1 / 5.0D;
      }
      paramGraphics2D.setPaint(localPaint);
    }
  }

  private void drawTickLine(Graphics2D paramGraphics2D, double paramDouble1, double paramDouble2, Color paramColor)
  {
    Paint localPaint = paramGraphics2D.getPaint();
    paramGraphics2D.setPaint(paramColor);
    double d1 = valueToAngle(paramDouble1);
    double d2 = this.meterMiddleX + (this.radius - 1.0D) * Math.cos(3.141592653589793D * d1 / 180.0D);
    double d3 = this.meterMiddleY - (this.radius - 1.0D) * Math.sin(3.141592653589793D * d1 / 180.0D);
    double d4 = this.meterMiddleX + paramDouble2 * Math.cos(3.141592653589793D * d1 / 180.0D);
    double d5 = this.meterMiddleY - paramDouble2 * Math.sin(3.141592653589793D * d1 / 180.0D);
    paramGraphics2D.draw(new Line2D.Double(d4, d5, d2, d3));
    paramGraphics2D.setPaint(localPaint);
  }

  private void drawTickLabel(Graphics2D paramGraphics2D, double paramDouble1, double paramDouble2, FRFont paramFRFont)
  {
    Font localFont = paramGraphics2D.getFont();
    if (this.meterStyle.isTickLabelsVisible())
    {
      String str;
      double d1 = valueToAngle(paramDouble1);
      if (this.meterStyle.getUnitFormat() != null)
        str = this.meterStyle.getUnitFormat().format(Double.valueOf(Utils.doubleToString(paramDouble1))).toString();
      else
        str = Utils.doubleToString(paramDouble1);
      paramGraphics2D.setFont(paramFRFont);
      FontMetrics localFontMetrics = paramGraphics2D.getFontMetrics();
      Rectangle2D localRectangle2D = getTextBounds(str, paramGraphics2D, localFontMetrics);
      double d2 = this.meterMiddleX + paramDouble2 * Math.cos(3.141592653589793D * d1 / 180.0D);
      double d3 = this.meterMiddleY - paramDouble2 * Math.sin(3.141592653589793D * d1 / 180.0D);
      if ((d1 == 90.0D) || (d1 == 270.0D))
      {
        d2 -= localRectangle2D.getWidth() / 2.0D;
        d3 += localRectangle2D.getHeight() / 4.0D;
      }
      else if ((d1 < 90.0D) || (d1 > 270.0D))
      {
        d2 -= localRectangle2D.getWidth() / 2.0D;
      }
      else if ((d1 > 90.0D) || (d1 < 270.0D))
      {
        d3 += localRectangle2D.getHeight() / 2.0D;
        d2 -= localRectangle2D.getWidth() / 2.0D;
      }
      paramGraphics2D.drawString(str, (float)d2, (float)d3);
    }
    paramGraphics2D.setFont(localFont);
  }

  private FRFont getTickLabelFont()
  {
    double d1 = this.meterStyle.getTickSize();
    if ((this.meterStyle.isTickLabelsVisible()) && (d1 != 0.0D))
    {
      Object localObject = null;
      double d2 = this.meterStyle.getStartValue();
      double d3 = this.meterStyle.getEndValue();
      DoubleDimension2D localDoubleDimension2D = new DoubleDimension2D(this.radius * 0.2D, this.radius * 0.2D);
      BigDecimal localBigDecimal = new BigDecimal(Double.toString(d2)).subtract(new BigDecimal(Double.toString(d1)));
      double d4 = d2;
      while (d4 <= d3)
      {
        String str;
        localBigDecimal = localBigDecimal.add(new BigDecimal(Double.toString(d1)));
        if (this.meterStyle.getUnitFormat() != null)
          str = this.meterStyle.getUnitFormat().format(Double.valueOf(Utils.doubleToString(localBigDecimal.doubleValue()))).toString();
        else
          str = Utils.doubleToString(localBigDecimal.doubleValue());
        FRFont localFRFont = getFont(localDoubleDimension2D, str);
        if (d4 == d2)
          localObject = localFRFont;
        else if (localFRFont.getSize() < localObject.getSize())
          localObject = localFRFont;
        d4 += d1;
      }
      return localObject;
    }
    return null;
  }

  private FRFont getFont(Dimension2D paramDimension2D, String paramString)
  {
    FRFont localFRFont = FRContext.getDefaultValues().getFRFont();
    localFRFont = localFRFont.applySize(6);
    Dimension2D localDimension2D = TextGlyph.calculateTextDimension(paramString, new TextAttr(localFRFont));
    do
    {
      if ((localDimension2D.getHeight() >= paramDimension2D.getHeight()) || (localDimension2D.getWidth() >= paramDimension2D.getWidth()))
        break label90;
      localFRFont = localFRFont.applySize(localFRFont.getSize() + 1);
      localDimension2D = TextGlyph.calculateTextDimension(paramString, new TextAttr(localFRFont));
    }
    while (localFRFont.getSize() <= 11);
    return localFRFont;
    label90: return localFRFont;
  }

  private Rectangle2D getTextBounds(String paramString, Graphics2D paramGraphics2D, FontMetrics paramFontMetrics)
  {
    Rectangle2D localRectangle2D = paramFontMetrics.getStringBounds(paramString, paramGraphics2D);
    LineMetrics localLineMetrics = paramFontMetrics.getFont().getLineMetrics(paramString, paramGraphics2D.getFontRenderContext());
    localRectangle2D.setRect(localRectangle2D.getX(), localRectangle2D.getY(), localRectangle2D.getWidth(), localLineMetrics.getHeight());
    return localRectangle2D;
  }

  public void setMeterStyle(MeterStyle paramMeterStyle)
  {
    this.meterStyle = paramMeterStyle;
  }

  public MeterStyle getMeterStyle()
  {
    return this.meterStyle;
  }

  public void setValue(double paramDouble)
  {
    this.value = paramDouble;
  }

  public double getValue()
  {
    return this.value;
  }

  public void setTitle(TextGlyph paramTextGlyph)
  {
    this.titleGlyph = paramTextGlyph;
  }

  public TextGlyph getTitle()
  {
    return this.titleGlyph;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Value"))
      {
        String str2;
        if ((str2 = paramXMLableReader.getAttr("value")) != null)
          setValue(Double.valueOf(str2).doubleValue());
      }
      else if (str1.equals("TextGlyph"))
      {
        this.titleGlyph = ((TextGlyph)paramXMLableReader.readXMLObject(new TextGlyph()));
      }
      else if (str1.equals("MeterStyle"))
      {
        setMeterStyle((MeterStyle)paramXMLableReader.readXMLObject(new MeterStyle()));
      }
      else if (str1.equals("Bounds"))
      {
        setBounds(ReportXMLUtils.readBounds(paramXMLableReader));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Meter");
    paramXMLPrintWriter.startTAG("Value").attr("value", getValue()).end();
    if (getTitle() != null)
      getTitle().writeXML(paramXMLPrintWriter);
    if (this.meterStyle != null)
      this.meterStyle.writeXML(paramXMLPrintWriter);
    if (this.bounds != null)
      ReportXMLUtils.writeBounds(paramXMLPrintWriter, this.bounds);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof Meter))
      return false;
    Meter localMeter = (Meter)paramObject;
    if (!(Equals.equals(localMeter.getBounds(), getBounds())))
      return false;
    if (localMeter.getValue() != getValue())
      return false;
    if (!(Equals.equals(localMeter.getMeterStyle(), getMeterStyle())))
      return false;
    return (Equals.equals(localMeter.getTitle(), getTitle()));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Meter localMeter = (Meter)super.clone();
    if (this.meterStyle != null)
      localMeter.setMeterStyle((MeterStyle)this.meterStyle.clone());
    if (this.bounds != null)
      localMeter.setBounds((Rectangle2D)this.bounds.clone());
    if (this.titleGlyph != null)
      localMeter.setTitle((TextGlyph)this.titleGlyph.clone());
    return localMeter;
  }
}